package pwd.allen.androiddemo.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import pwd.allen.androiddemo.R;

public class JumpActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText;
    private Button btnDial;
    private Button btnSms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jump);
        editText = findViewById(R.id.editText);

        // 获取本机手机号
//        SubscriptionManager subscriptionManager = (SubscriptionManager) this.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
//        if (checkSelfPermission(Manifest.permission.READ_PHONE_NUMBERS) == PackageManager.PERMISSION_GRANTED) {
//            String phoneNumber = subscriptionManager.getPhoneNumber(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
//            editText.setText(phoneNumber);
//        }
        // 好像只能获取很老版本的sim卡
//        TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//        String phoneNumber = mTelephonyMgr.getLine1Number();
//        if (phoneNumber != null && "".equals(phoneNumber.trim())) {
//            editText.setText(phoneNumber);
//        }

        btnDial = findViewById(R.id.dial);
        btnDial.setOnClickListener(this);
        btnSms = findViewById(R.id.sms);
        btnSms.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String phone = editText.getText().toString();
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.dial:
                // 拨号
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + phone));
                startActivity(intent);
                break;
            case R.id.sms:
                // 发短信
                intent.setAction(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("smsto:" + phone));
                startActivity(intent);
                break;

            //<editor-fold desc="打开自己的其他应用的activity（该activity需要exported）；action category对应AndroidManifest里对应activity的intent-filter配置">
//            intent.setAction("android.intent.action.MAIN");
//            intent.addCategory(Intent.CATEGORY_LAUNCHER);
//            startActivity(intent);
            //</editor-fold>

        }
    }
}
